package cn.jilian.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.jilian.common.domain.R;
import cn.jilian.entity.Menu;
import cn.jilian.entity.User;
import cn.jilian.security.util.ShiroUtils;
import cn.jilian.sys.domain.MaceMap;
import cn.jilian.sys.service.MaceService;
import cn.jilian.sys.service.MenuService;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/mace")
public class MaceController {
	
	@Autowired
	private MaceService maceService;
	@Autowired
	private MenuService menuService;
	
	@ApiOperation(value="保存权限")
	@RequiresPermissions("system:mace:save")
	@RequestMapping(value="/add",method= {RequestMethod.GET,RequestMethod.POST})
	public R saveUserMenuMace(@RequestParam("fid")Long fid,@RequestParam("uid")Long uid,
			@RequestParam("act")Integer act,@RequestParam("meta")Long meta) {
		try {
			maceService.save(fid, uid, act, meta);
		return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	@ApiOperation(value="加载登录用户可访问的菜单树")
	@RequestMapping(value="/getMenuTree",method= {RequestMethod.GET,RequestMethod.POST})
	public List<Menu> getMenuTree(HttpSession session){
		User user=ShiroUtils.getUser();
		return menuService.loadUserMenu(user, Menu.RANK);
	}
	
	@ApiOperation(value="查看指定用户权限集合")
	@GetMapping("/getMaceMap")
	public R getMaceMap(@RequestParam("uid")Long uid,@RequestParam("rank")Integer rank) {
		try {
			Map<Long,Integer> maceMap=maceService.getMaceMap(uid,rank);
			return R.ok(maceMap);
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	@ApiOperation(value="保存用户权限集合")
	@PostMapping(value="/batchSave")
	public R batchSaveMaceMap(@RequestBody MaceMap maceData) {
		try {
		maceService.saveMaceMap(maceData.getUid(),maceData.getMaceMap(),maceData.getRank(),maceData.getMeta());
		return R.ok(maceService.getMaceMap(maceData.getUid(), maceData.getRank()));
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	
}
